/*
 * 文件名：VerifyCodeServiceImpl.java
 * 版权：Copyright by www.huawei.com
 * 描述：
 * 修改人：hry
 * 修改时间：2016年6月14日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.zcsy.manage.service.base.impl;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zcsy.commons.constants.Constants;
import com.zcsy.commons.model.RequestJson;
import com.zcsy.commons.sms.dlife.DlifeSmsConfig;
import com.zcsy.commons.unique.UniqueNumberUtils;
import com.zcsy.manage.model.base.VerifyCode;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.persistence.base.VerifyCodeDao;
import com.zcsy.manage.service.base.IVerifyCodeService;
import com.zcsy.manage.service.sms.DlifeSmsService;

import net.sf.ezmorph.object.SwitchingMorpher;

/**
 * 
 * @Title: VerifyCodeServiceImpl.java
 * @Package： com.zcsy.manage.service.base.impl 
 * @Description: 验证码service层接口实现类
 *
 * @author lilan 1063972142@qq.com
 * @Copyright: 2015 武汉恒数利通信息技术有限公司 All rights reserved.
 * @date 2016年6月14日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@Service
public class VerifyCodeServiceImpl implements IVerifyCodeService{

	@Autowired
	private VerifyCodeDao verifyCodeDao;
	
	@Autowired
    private DlifeSmsService dlifesmsService;

	@Override
	public int insert(VerifyCode verifyCode) {
		return verifyCodeDao.insert(verifyCode);
	}

	@Override
	public VerifyCode selectByTime(VerifyCode verifyCode) {
		return verifyCodeDao.selectByTime(verifyCode);
	}
	
	/**
	 * 给手机发送验证码短信
	 */
	@Override
	public RequestJson sendCodeMessage(User user, String phone, String phoneCode, RequestJson result){
		VerifyCode verifycode = new VerifyCode();
		    verifycode.setCode(phoneCode);
		    //创建时间
		    Date date = new Date();
		    SimpleDateFormat from = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		    String times = from.format(date);
		    verifycode.setCreateTime(times);
		    //过期时间
		    date.setMinutes(date.getMinutes()+Integer.valueOf(DlifeSmsConfig.SMS_EXPIRE_TIME));//给当前时间加30分钟后的时间
		    times=from.format(date);
		    verifycode.setExpireTime(times);
		    verifycode.setUserAccount(user.getLoginname());
		    verifycode.setUserType(Constants.BUSINESS_TYPE);
		    verifycode.setOperateType(Constants.BIND_PHONE);
		    verifycode.setPhone(phone);
		    verifycode.setId(UniqueNumberUtils.get18UniqueNumber());
		    insert(verifycode);
		    
		    String msg = "";
		    if(null != user && null != user.getUserType()) {
		        switch (user.getUserType()) {
                case Constants.BUSINESS_TYPE:
                    msg = "尊敬的商户您好，您的验证码是" + phoneCode+ "，三十分钟内有效，请勿向任何人泄露";
                    break;
                case Constants.OWNER_TYPE:
                    msg = "尊敬的业主您好，您的验证码是" + phoneCode + "，三十分钟内有效，请勿向任何人泄露";
                    break;
                default:
                    msg = "您好，您的验证码是" + phoneCode + "，三十分钟内有效，请勿向任何人泄露";
                    break;
		        } 
		    }else {
                msg = "您好，您的验证码是" + phoneCode + "，三十分钟内有效，请勿向任何人泄露";
		    }
		    dlifesmsService.sendMsg(phone, msg, null);
		
		//取出最新的验证码
		    String newCode = selectByTime(verifycode).getCode();
		    result.setObj(newCode);
		    
		    return result;
	}
	
}
